﻿using System;
using Medicalsystem.MCP.FormDesigner.Common;
using Medicalsystem.MCP.FormDesigner.Interface;

namespace Medicalsystem.MCP.FormDesigner.Property
{
    public class ModelPropertyXML : IXMLConvertor
    {
        /// <summary>
        /// 属性串的目标对象
        /// </summary>
        protected System.Windows.Forms.Form _form;

        /// <summary>
        /// 模板基本属性的XML转换
        /// </summary>
        /// <param name="Form"></param>
        public ModelPropertyXML(System.Windows.Forms.Form Form)
        {
            _form = Form;
        }

        #region IXMLConvertor 成员

        public string ToXML()
        {
            return "<Text>" + ESCEncrypt.escape(_form.Text) + "</Text>" +
                       "<Height>" + _form.Height.ToString() + "</Height>" +
                       "<Width>" + _form.Width.ToString() + "</Width>" +
                       "<AutoScroll>" + BoolToString(_form.AutoScroll) + "</AutoScroll>" +
                       "<BackColor>" + _form.BackColor.ToArgb().ToString() + "</BackColor>" +
                       "<BackgroundImage>" + ConvertImageToString(_form.BackgroundImage) + "</BackgroundImage>" +
                       "<BackgroundImageLayout>" + ((int)_form.BackgroundImageLayout).ToString() + "</BackgroundImageLayout>" +
                       "<ChangeSize>" + GetChangeSize() + "</ChangeSize>";
        }

        public void FromXML(string strXML)
        {
            if (strXML.Length > 0)
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.LoadXml(string.Format("<Property>{0}</Property>", strXML));

                FromXML(doc.DocumentElement.ChildNodes);
            }
        }

        public void FromXML(System.Xml.XmlNodeList xns)
        {
            if (xns != null && xns.Count > 0)
            {
                foreach (System.Xml.XmlNode XN in xns)
                {
                    switch (XN.Name.ToLower())
                    {
                        case "text":
                            if (XN.InnerText.Length > 0)
                            {
                                _form.Text = ESCEncrypt.unescape(XN.InnerText);
                            }
                            break;

                        case "height":
                            _form.Height = Convert.ToInt32(XN.InnerText);
                            break;

                        case "width":
                            _form.Width = Convert.ToInt32(XN.InnerText);
                            break;

                        case "autoscroll":
                            _form.AutoScroll = StringToBool(XN.InnerText);
                            break;

                        case "backcolor":
                            _form.BackColor = System.Drawing.Color.FromArgb(Convert.ToInt32(XN.InnerText));
                            break;

                        case "backgroundimage":
                            _form.BackgroundImage = XN.InnerText.Length > 0 ? ConvertStringToImage(XN.InnerText) : null;
                            break;

                        case "backgroundimagelayout":
                            _form.BackgroundImageLayout = (System.Windows.Forms.ImageLayout)Convert.ToInt32(XN.InnerText);
                            break;

                        case "changesize":
                            if (XN.InnerText.Trim() == "1")
                            {
                                _form.MaximizeBox = true;
                                _form.MinimizeBox = true;
                            }
                            else
                            {
                                _form.MaximizeBox = false;
                                _form.MinimizeBox = false;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        #endregion IXMLConvertor 成员

        private string GetChangeSize()
        {
            if (this._form.MaximizeBox)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        /// <summary>
        /// Bool型转换字符型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected string BoolToString(bool value)
        {
            if (value)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        /// <summary>
        /// 字符型转换Bool型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected bool StringToBool(string value)
        {
            if (value.Trim().Equals("0"))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 字符串转换为图片
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private System.Drawing.Image ConvertStringToImage(string str)
        {
            try
            {
                byte[] ImageBytes = Convert.FromBase64String(str);
                System.IO.MemoryStream ImageMem = new System.IO.MemoryStream(ImageBytes);
                return System.Drawing.Image.FromStream(ImageMem);
            }
            catch
            { }

            return null;
        }

        /// <summary>
        /// 图片转换为字符串
        /// </summary>
        /// <param name="IMG"></param>
        /// <returns></returns>
        private string ConvertImageToString(System.Drawing.Image IMG)
        {
            if (IMG == null) return "";

            try
            {
                System.IO.MemoryStream ImageMem = new System.IO.MemoryStream();
                IMG.Save(ImageMem, IMG.RawFormat);
                byte[] ImageBytes = ImageMem.GetBuffer();
                return Convert.ToBase64String(ImageBytes);
            }
            catch
            { }

            return "";
        }
    }
}